package Model;

/*CREATE  TABLE IF NOT EXISTS `mydb`.`Turnos` (
  `idTurnos` INT NOT NULL AUTO_INCREMENT ,
  `descricao` VARCHAR(99) NOT NULL ,
  `horaInicio` TIME NOT NULL ,
  `horaFim` TIME NOT NULL ,
  PRIMARY KEY (`idTurnos`) )
ENGINE = InnoDB;*/
public class Turnos {
    //<editor-fold defaultstate="collapsed" desc="Connection">
    private static com.mysql.jdbc.Connection connection;

    public static com.mysql.jdbc.Connection getConnection() {
        return connection;
    }

    public static void setConnection(com.mysql.jdbc.Connection connection) {
        Turnos.connection = connection;
    }
    //</editor-fold>
    //<editor-fold defaultstate="collapsed" desc="Variable Declaration">
    private int idTurnos;
    private String descricao;
    private java.sql.Time horaInicio;
    private java.sql.Time horaFim;
    //</editor-fold>
    //<editor-fold defaultstate="collapsed" desc="Constructor">
    public Turnos(int idTurnos) {
        try {
            com.mysql.jdbc.ResultSet rs = (com.mysql.jdbc.ResultSet) connection.createStatement().executeQuery("SELECT * FROM itens WHERE idTurnos = '" + idTurnos + "'");
            rs.next();
            this.idTurnos = rs.getInt("idTurnos");
            this.descricao = rs.getString("descricao");
            this.horaInicio = rs.getTime("horaInicio");
            this.horaFim = rs.getTime("horaFim");
        } catch (Exception e) {
        }
    }
    //</editor-fold>
    //<editor-fold defaultstate="collapsed" desc="toString">
    @Override
    public String toString() {
        String s="Turnos:";
            s += "idTurnos=" + idTurnos;
        return s;
    }
    //</editor-fold>
    //<editor-fold defaultstate="collapsed" desc="Getter">
    public int idTurnos() {
        return this.idTurnos;
    }

    public String descricao() {
        return this.descricao;
    }

    public java.sql.Time horaInicio() {
        return this.horaInicio;
    }

    public java.sql.Time horaFim() {
        return this.horaFim;
    }

    //</editor-fold>
    //<editor-fold defaultstate="collapsed" desc="Setter">
    public boolean idTurnos(int idTurnos) {
        boolean ok = update("idTurnos", idTurnos);
        if (ok) {
            this.idTurnos = idTurnos;
        }
        return ok;
    }
    public boolean descricao(String descricao) {
        boolean ok = update("descricao", descricao);
        if (ok) {
            this.descricao = descricao;
        }
        return ok;
    }
    public boolean horaInicio(java.sql.Time horaInicio) {
        boolean ok = update("horaInicio", horaInicio);
        if (ok) {
            this.horaInicio = horaInicio;
        }
        return ok;
    }
    public boolean horaFim(java.sql.Time horaFim) {
        boolean ok = update("horaFim", horaFim);
        if (ok) {
            this.horaFim = horaFim;
        }
        return ok;
    }
    //</editor-fold>
    //<editor-fold defaultstate="collapsed" desc="Update">
    /**
     *
     * @param campo
     * @param valor
     * @return
     */
    public boolean update(String campo, Object valor) {
        try {
            String sql = "UPDATE Turnos \n"
                    + "SET " + campo + " = '";
            if (valor instanceof String) {
                sql += (String) valor;
            } else if (valor instanceof Integer) {
                sql += (Integer) valor;
            } else if (valor instanceof java.sql.Timestamp) {
                sql += ((java.sql.Timestamp) valor).toString().split("\\.")[0];
            } else if (valor instanceof java.util.Date) {
                java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat(
                        "yyyy-MM-dd hh:mm:ss");
                sql += sdf.format((java.util.Date) valor);
            } else {
                sql += valor.toString();
            }
            sql += "'\n WHERE \n idTurnos = '" + idTurnos + "'";
            com.mysql.jdbc.Statement st =
                    (com.mysql.jdbc.Statement) connection.createStatement();
            int x = st.executeUpdate(sql);
            if (x == 1) {
                return true;
            }
        } catch (Exception ex) {
        }
        return false;
    }
    //</editor-fold>
    //<editor-fold defaultstate="collapsed" desc="Search">
    public static java.util.ArrayList<Turnos> searchIdTurnos(int valor) {
        return search("idTurnos", valor+"");
    }
    public static java.util.ArrayList<Turnos> searchDescricao(String valor) {
        return search("descricao", valor+"");
    }
    public static java.util.ArrayList<Turnos> searchHoraInicio(java.sql.Time valor) {
        return search("horaInicio", valor+"");
    }
    public static java.util.ArrayList<Turnos> searchHoraFim(java.sql.Time valor) {
        return search("horaFim", valor+"");
    }

    public static java.util.ArrayList<Turnos> search(String campo, String valor) {
        try {
            java.util.ArrayList<Turnos> retorno = new java.util.ArrayList<>();
            String sql = "SELECT idTurnos FROM Turnos "
                    + "WHERE " + campo + " LIKE '%" + valor + "%' "
                    + "ORDER BY " + campo;
            com.mysql.jdbc.ResultSet rs = (com.mysql.jdbc.ResultSet) connection.createStatement().executeQuery(sql);
            while (rs.next()) {
                retorno.add(new Turnos(rs.getInt("idTurnos")));
            }
            return retorno;
        } catch (Exception e) {
        }
        return null;
    }
    //</editor-fold>
    //<editor-fold defaultstate="collapsed" desc="New">
    public static Turnos newTurnos(int idTurnos, String descricao, java.sql.Time horaInicio, java.sql.Time horaFim){
            String sql = "INSERT INTO Turnos (idTurnos, descricao, horaInicio, horaFim)"
                    + " VALUES "
                    + "('"+idTurnos+"', '"+descricao+"', '"+horaInicio+"', '"+horaFim+"');";
        com.mysql.jdbc.Statement stmt = null;
        com.mysql.jdbc.ResultSet rs = null;
        try {
            stmt = (com.mysql.jdbc.Statement) connection.createStatement(java.sql.ResultSet.TYPE_FORWARD_ONLY,
                    java.sql.ResultSet.CONCUR_UPDATABLE);
            stmt.executeUpdate(sql, com.mysql.jdbc.Statement.RETURN_GENERATED_KEYS);
            rs = (com.mysql.jdbc.ResultSet) stmt.getGeneratedKeys();
            if (rs.next()) {
                return new Turnos(rs.getInt("idTurnos"));
            } else {
                return new Turnos(idTurnos);
            }
        } catch (Exception e) {
            e.printStackTrace();
            if (rs != null) {
                try {
                    rs.close();
                } catch (Exception ex) {
                    // ignore
                }
            }
            if (stmt != null) {
                try {
                    stmt.close();
                } catch (Exception ex) {
                    // ignore
                }
            }
        }
        return null;
    }
    //</editor-fold>
}